    //
//  TodayFXViewController.m
//  一点互助
//
//  Created by rimi on 16/8/24.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "TodayFXViewController.h"
#import "UIViewController+NavagitionBar.h"
#import "ConfigDefine.h"
#import "ResponseManager.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <Masonry.h>
#import "FXTableViewCell.h"
#import "PastFXTableViewCell.h"

@interface TodayFXViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UIButton *todayBtn;
@property (strong, nonatomic) UIButton *pastBtn;
@property (strong, nonatomic) UIView *naviView;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) NSMutableArray *newDataSource;
@property (assign, nonatomic) CGRect frame;
@end

@implementation TodayFXViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupBackItem];
    [self setupNavigationBar];
    [self getNetWorkRequest];
    [self.view addSubview:self.tableView];
    UINib *nib = [UINib nibWithNibName:@"FXTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"FXTableViewCell"];
    [self.todayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UINib *nib2 = [UINib nibWithNibName:@"PastFXTableViewCell" bundle:nil];
    [self.tableView registerNib:nib2 forCellReuseIdentifier:@"PastFXTableViewCell"];
    
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;

}
-(void)olderNetwork{
    __weak typeof(self) myself = self;
    [ResponseManager startGetRequestWithURLString:kOlderFXURL parameters:nil completionHandler:^(FBResponse *response) {
        if (response.success) {
//            PLLOG(@"%@",response.resultValue);
            myself.newDataSource = [response.resultValue mutableCopy];
            [myself.tableView reloadData];
        }
    }];

}
-(void)setupNavigationBar{
    [self.view addSubview:self.naviView];
    [self.naviView addSubview:self.todayBtn];
    [self.naviView addSubview:self.pastBtn];
    self.navigationItem.titleView = self.naviView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
-(void)getNetWorkRequest{
    __weak typeof(self) myself = self;
    [ResponseManager startGetRequestWithURLString:kTodayLuckyBagFXURL parameters:nil completionHandler:^(FBResponse *response) {
        if (response.success) {
//            PLLOG(@"%@",response.resultValue);
            myself.dataSource = [response.resultValue mutableCopy];
            [myself.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        }
    }];
}
-(UIView *)naviView{
    if (!_naviView) {
        _naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-40, 44)];
    }
    return _naviView;
}
-(UIButton *)todayBtn{
    if (!_todayBtn) {
        _todayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _todayBtn.frame = CGRectMake(0, self.naviView.bounds.size.height/2-10, self.naviView.bounds.size.width/2, self.naviView.bounds.size.height/2);
        [_todayBtn setTitle:@"今日返现" forState:UIControlStateNormal];
        [_todayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        [_todayBtn addTarget:self action:@selector(todayBtnTaped:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _todayBtn;
}
-(void)todayBtnTaped:(UIButton *)todayBtn{
    PLLOG(@"today");
    [todayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.pastBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.newDataSource removeAllObjects];
    [self getNetWorkRequest];

}
-(void)pastBtnTaped:(UIButton *)pastBtnTaped{
    PLLOG(@"past");
    [pastBtnTaped setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.todayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.dataSource removeAllObjects];
    [self olderNetwork];
}
-(UIButton *)pastBtn{
    if (!_pastBtn) {
        _pastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _pastBtn.frame = CGRectMake(self.naviView.bounds.size.width/2, self.naviView.bounds.size.height/2-10,self.naviView.bounds.size.width/2, self.naviView.bounds.size.height/2);
        [_pastBtn setTitle:@"往期详情" forState:UIControlStateNormal];
        [_pastBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_pastBtn addTarget:self action:@selector(pastBtnTaped:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pastBtn;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


#pragma mark - UITableViewDaTaSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataSource.count == 0) {
        return self.newDataSource.count;
    }else{
        return self.dataSource.count;
    }
}

//设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    PLLOG(@"%lu",(unsigned long)self.newDataSource.count);
//    PLLOG(@"%lu",(unsigned long)self.dataSource.count);
    if (self.newDataSource.count == 0) {
        FXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FXTableViewCell" forIndexPath:indexPath];
        NSString *str = self.dataSource[indexPath.row][@"thumb"];
        NSString *newURL = [NSString stringWithFormat:@"http://www.scxxhs.com/huzhu/%@",str];
        NSURL *url = [NSURL URLWithString:newURL];
        [cell.image sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"个人中心-默认头像"]];
        cell.text.text = [NSString stringWithFormat:@"%@%@获得%@元返现",self.dataSource[indexPath.row][@"user_nicename"],self.dataSource[indexPath.row][@"mobile"],self.dataSource[indexPath.row][@"price"]];
        cell.image.layer.cornerRadius =  cell.image.frame.size.height/2;
        cell.image.layer.masksToBounds = YES;
        self.frame = cell.image.frame;
        return cell;
    }else{
        PastFXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PastFXTableViewCell" forIndexPath:indexPath];
        cell.dateLabel.text = self.newDataSource[indexPath.row][@"date"];
        cell.countLabel.text = self.newDataSource[indexPath.row][@"num"];
        cell.totalLabel.text = self.newDataSource[indexPath.row][@"price"];
        return cell;
    }
}

#pragma mark - UITableViewDelegate methods



//设置单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataSource.count == 0) {
        return 80 * kScreenHeightScale;
    }else{
        return 60 * kScreenHeightScale;
    }

}

#pragma mark - Getter methods
- (UITableView *)tableView{
    if (!_tableView) {
        //初始化表格视图
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}
-(NSMutableArray *)newDataSource{
    if (!_newDataSource) {
        _newDataSource = [NSMutableArray array];
    }
    return _newDataSource;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
